<template>
  <div class="ly-page">
    <ul class="ly-list-input">
  <li>
    <label class="ly-label">名称：</label>
    <el-input class="ly-input ly-link" v-model="consult.title" size="small" placeholder="请输入名称"></el-input>
  </li>
  <li>
    <label class="ly-label">简介：</label>
    <el-input class="ly-input ly-textarea" type="textarea" v-model="consult.remark" size="small" placeholder="请输入简介"></el-input>
  </li>
  <li>
    <label class="ly-label">类型：</label>
    <el-radio class="ly-radio" v-model="consult.type" label="行业新闻">行业新闻</el-radio>
    <el-radio class="ly-radio" v-model="consult.type" label="企业新闻">企业新闻</el-radio>
  </li>
  <li>
    <label class="ly-label">封面图片：</label>
    <el-upload class="avatar-uploader" :action="action" :show-file-list="false"
               :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
      <img class="ly-img" title="上传图片" :src="consult.picture"/>
    </el-upload>
  </li>
  <li>
    <label class="ly-label">服务内容：</label>
  </li>
  <div class="editor-container">
    <kindeditor ref="kindeditor" :content="consult.content"></kindeditor>
  </div>
</ul>
  <el-button @click="$router.back(-1)">返回</el-button>
  <el-button @click="$router.back(-1)">取消</el-button>
  <el-button type="primary" @click="submit">保存</el-button>
  </div>
</template>

<script>
  import config from '@/config'
  import {update, insert} from '@/api/news'
  import {mapGetters} from 'vuex'

  export default {
    data() {
      return {
        consult: {
          picture: require('@/assets/images/icon_img.png')
        },
        action: config.baseURL + '/upload/upload',
      }
    },
    computed: {
      ...mapGetters(['editInfo'])
    },
    methods: {
      // 获取编辑器内容
      getContent() {
        console.log(this.$refs.kindeditor.outContent)
      },
      // 选中的分类 内容
      changeClient(e) {
        console.log(e)
      },
      handleAvatarSuccess(res, file) {
        this.consult.picture = res.data
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传图片格式错误!');
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      submit() {
        let data = this.editInfo
        this.editInfo.content = this.$refs.kindeditor.outContent
        this.editInfo.title = this.consult.title
        this.editInfo.remark = this.consult.remark
        this.editInfo.type = this.consult.type
        this.editInfo.picture = this.consult.picture
        let updateData
        if (this.$route.query.type) {
          updateData = update
        } else {
          updateData = insert
          delete this.editInfo.id
        }
        updateData(data).then(response => {
          let result = response.data
          if (result.code === '0000') {
            this.$message.success(result.msg)
            if (this.$route.query.type) {
              this.business = {}
            }
          } else {
            this.$message.error(result.msg)
          }
        })
      },
    },
    created() {
      let vm = this
      setTimeout(() => {
        if (vm.$route.query.type) {
          vm.consult = vm.editInfo
        }
      }, 500)
    },
    beforeDestroy() {
      this.consult = {}
    }
  }
</script>

<style lang="scss" scoped>

</style>